{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4ce64883-1b1c-4ae2-b67e-b2d2fc67e34b",
   "metadata": {},
   "source": [
    "# LLaMA Factory 微调实践：微调 Qwen3 构建西游记大模型\n",
    "\n",
    "[LLaMA Factory](https://github.com/hiyouga/LLaMA-Factory) 是一款开源低代码大模型微调框架，集成了业界最广泛使用的微调技术，支持通过 Web UI 界面零代码微调大模型，目前已经成为开源社区内最受欢迎的微调框架之一，GitHub 星标超过 3 万。本教程将基于通义千问团队开源的新一代多模态大模型 Qwen3，介绍如何使用 PAI 平台及 LLaMA Factory 训练框架完成西游记大模型的构建。 "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "329289d4-ddb4-41a9-b628-b74a24bec4ff",
   "metadata": {},
   "source": [
    "## 运行环境要求\n",
    "\n",
    "- GPU 推荐使用 24GB 显存的 A10（`ecs.gn7i-c8g1.2xlarge`）或更高配置\n",
    "- 镜像选择 DSW 官方镜像 `modelscope:1.18.0-pytorch2.3.0-gpu-py310-cu121-ubuntu22.04`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "716a41bb-a415-4d89-864c-516d3e584160",
   "metadata": {},
   "source": [
    "## 1. 安装 LLaMA Factory\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9b235f24-dda4-43fb-a59b-1adae6656e3d",
   "metadata": {},
   "source": [
    "首先，拉取 LLaMA-Factory 项目到 DSW 实例。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b24d397f-3059-4fed-9283-c90a0d439a76",
   "metadata": {
    "ExecutionIndicator": {
     "show": true
    },
    "execution": {
     "iopub.execute_input": "2025-06-25T05:33:41.929002Z",
     "iopub.status.busy": "2025-06-25T05:33:41.928716Z",
     "iopub.status.idle": "2025-06-25T05:33:51.956137Z",
     "shell.execute_reply": "2025-06-25T05:33:51.955702Z",
     "shell.execute_reply.started": "2025-06-25T05:33:41.928983Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "!git clone --depth 1 https://github.com/hiyouga/LLaMA-Factory.git\n",
    "%cd LLaMA-Factory"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a433687-f7a3-45c3-aba1-317b66f37707",
   "metadata": {},
   "source": [
    "接着，我们安装 LLaMA-Factory 依赖环境。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "93053fb6-40a6-4986-91ce-d92a4e83898c",
   "metadata": {
    "ExecutionIndicator": {
     "show": true
    },
    "execution": {
     "iopub.execute_input": "2025-06-25T05:34:07.691005Z",
     "iopub.status.busy": "2025-06-25T05:34:07.690710Z",
     "iopub.status.idle": "2025-06-25T05:34:22.876331Z",
     "shell.execute_reply": "2025-06-25T05:34:22.875881Z",
     "shell.execute_reply.started": "2025-06-25T05:34:07.690986Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "!pip install -e .[metrics]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "434b7624-7149-4fb3-a49a-ac03949d3438",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-06-25T05:34:28.577590Z",
     "iopub.status.busy": "2025-06-25T05:34:28.577343Z",
     "iopub.status.idle": "2025-06-25T05:34:39.182309Z",
     "shell.execute_reply": "2025-06-25T05:34:39.181862Z",
     "shell.execute_reply.started": "2025-06-25T05:34:28.577572Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "!llamafactory-cli version"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "59ea93b3",
   "metadata": {},
   "source": [
    "安装 swanlab 训练跟踪与可视化工具："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cc60f33a",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install swanlab"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a53f1f8b-7a4c-438b-a39d-c981d5e04171",
   "metadata": {},
   "source": [
    "## 2. 数据集"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1df32c9a-2189-4e7b-bd34-7c4ec58bf1aa",
   "metadata": {},
   "source": [
    "LLaMA-Factory 项目内置了丰富的数据集，放在了 `data` 目录下。您可以跳过本步骤，直接使用内置数据集。您也可以准备自定义数据集，将数据处理为框架特定的格式，放在 `data` 下，并且修改 `dataset_info.json` 文件。\n",
    "\n",
    "本教程准备了一份多轮对话数据集，运行下述命令下载数据。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "624cba8b-99bd-4b64-b3cf-17ef3d6f8985",
   "metadata": {
    "ExecutionIndicator": {
     "show": true
    },
    "execution": {
     "iopub.execute_input": "2025-06-25T05:34:52.274034Z",
     "iopub.status.busy": "2025-06-25T05:34:52.273761Z",
     "iopub.status.idle": "2025-06-25T05:34:57.990515Z",
     "shell.execute_reply": "2025-06-25T05:34:57.990098Z",
     "shell.execute_reply.started": "2025-06-25T05:34:52.274016Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "!cd .. && mv xyj_data LLaMA-Factory/xyj_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a84e1207-3214-4366-bd0b-9073866da17b",
   "metadata": {
    "ExecutionIndicator": {
     "show": true
    },
    "execution": {
     "iopub.execute_input": "2025-06-25T05:39:02.598777Z",
     "iopub.status.busy": "2025-06-25T05:39:02.598523Z",
     "iopub.status.idle": "2025-06-25T05:39:02.601715Z",
     "shell.execute_reply": "2025-06-25T05:39:02.601255Z",
     "shell.execute_reply.started": "2025-06-25T05:39:02.598764Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "!cd LLaMA-Factory"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "539ba909-dc91-4e51-8a82-1bf9bea3ae1c",
   "metadata": {
    "ExecutionIndicator": {
     "show": true
    },
    "execution": {
     "iopub.execute_input": "2025-06-25T06:20:39.177772Z",
     "iopub.status.busy": "2025-06-25T06:20:39.177499Z",
     "iopub.status.idle": "2025-06-25T06:20:39.285732Z",
     "shell.execute_reply": "2025-06-25T06:20:39.285297Z",
     "shell.execute_reply.started": "2025-06-25T06:20:39.177755Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "!ls"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d73cf52d-0e68-4ef4-be03-ffad797f9155",
   "metadata": {},
   "source": [
    "## 3. 模型微调\n",
    "\n",
    "### 3.1 启动 Web UI\n",
    "\n",
    "做好前序准备工作后，直接运行下述命令就可以启动 Web UI。这里用到的环境变量解释如下：\n",
    "\n",
    "- `USE_MODELSCOPE_HUB` 设为 1，表示模型从 ModelScope 魔搭社区下载。避免从 HuggingFace 下载模型导致网速不畅。\n",
    "\n",
    "点击返回的 URL 地址，进入 Web UI 页面。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "988d2003-63f3-46e0-abc5-a4a7766d50cb",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2025-06-25T06:40:04.313224Z",
     "iopub.status.busy": "2025-06-25T06:40:04.312897Z"
    },
    "tags": []
   },
   "outputs": [],
   "source": [
    "!USE_MODELSCOPE_HUB=1 llamafactory-cli webui"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f683f400-a60f-4ecd-bcff-a4c684b2c720",
   "metadata": {},
   "source": [
    "### 3.2 配置参数\n",
    "进入 WebUI 后，可以切换语言到中文（zh）。首先配置模型，本教程选择 **Qwen3-8B** 模型，微调方法修改为 **LoRA**，针对小模型使用全参微调方法能带来更好的效果。\n",
    "\n",
    "可以点击「预览数据集」。点击关闭返回训练界面。\n",
    "\n",
    "\n",
    "设置学习率为 **3e-4**，训练轮数为 **3**，更改计算类型为 **pure_bf16**，梯度累积为 **2**，有利于模型拟合。\n",
    "\n",
    "在其他参数设置区域修改保存间隔为 **1000**，节省硬盘空间。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "31ca5e02-f52b-4002-ad0a-2329ce3917f9",
   "metadata": {},
   "source": [
    "### 3.3 启动微调\n",
    "\n",
    "将输出目录修改为 `train_qwen8_xyj`，训练后的模型权重将会保存在此目录中。点击「预览命令」可展示所有已配置的参数，您如果想通过代码运行微调，可以复制这段命令，在命令行运行。\n",
    "\n",
    "点击「开始」启动模型微调。\n",
    "\n",
    "启动微调后需要等待一段时间，待模型下载完毕后可在界面观察到训练进度和损失曲线。模型微调大约需要 14 分钟，显示“训练完毕”代表微调成功。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "746a321f-99fd-45c9-bd51-537c91e3e225",
   "metadata": {},
   "source": [
    "## 4. 模型对话\n",
    "\n",
    "选择「Chat」栏，将**检查点路径**改为 `train_qwen8_xyj`，点击「加载模型」即可在 Web UI 中和微调后的模型进行对话。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6982a7df",
   "metadata": {},
   "source": [
    "下载模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1b6a72fd",
   "metadata": {},
   "outputs": [],
   "source": [
    "!zip -r 1.zip Qwen3-8B-SRM-XYJ-V1"
   ]
  }
 ],
 "metadata": {
  "dsw_sample": {
   "buildId": "1126",
   "pipeline": "pai-dsw-examples-master"
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.11"
  },
  "share": {
   "datetime": "2024-09-18T08:29:01.728Z",
   "image": {
    "name": "modelscope:1.18.0-pytorch2.3.0-gpu-py310-cu121-ubuntu22.04",
    "url": "dsw-registry-vpc.cn-beijing.cr.aliyuncs.com/pai/modelscope:1.18.0-pytorch2.3.0-gpu-py310-cu121-ubuntu22.04"
   },
   "instance": "dsw-5b1711da891a398f",
   "spec": {
    "id": "ecs.gn7i-c32g1.8xlarge",
    "type": "GPU"
   },
   "uid": "1042815074264007"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
